﻿namespace Pub.Class
{
    using System;
    using System.Runtime.InteropServices;
    using System.Text;

    public class TopTopPagerSQL : IPagerSQL
    {
        public PagerSql GetSQL(int pageIndex, int pageSize, string tableName, string pk = "*", string fieldList = "*", string where = "", string groupBy = "", string orderBy = "")
        {
            PagerSql sql = new PagerSql();
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            builder.AppendFormat("count({0}) as total ", pk);
            if (!tableName.IsNullEmpty())
            {
                builder.AppendFormat("from {0} ", tableName);
            }
            if (!where.IsNullEmpty())
            {
                builder.AppendFormat("where {0} ", where);
            }
            if (!groupBy.IsNullEmpty())
            {
                builder.AppendFormat("group by {0} ", groupBy);
            }
            sql.CountSql = builder.ToString();
            StringBuilder sb = new StringBuilder();
            foreach (string str in orderBy.Split(new char[] { ',' }))
            {
                string str2 = str.Trim();
                if (str2.EndsWith(" desc", true, null))
                {
                    sb.AppendFormat("{0} {1},", str2.Left(str2.Length - 5), "asc");
                }
                else
                {
                    sb.AppendFormat("{0} {1},", str2.EndsWith(" asc", true, null) ? str2.Left((str2.Length - 4)) : str2, "desc");
                }
            }
            sb.RemoveLastChar(",");
            builder.Clear();
            builder.Append("select ");
            if (pageSize == 1)
            {
                builder.AppendFormat("top {0} ", pageSize);
                builder.AppendFormat("{0} ", fieldList);
                if (!tableName.IsNullEmpty())
                {
                    builder.AppendFormat("from {0} ", tableName);
                }
                if (!where.IsNullEmpty())
                {
                    builder.AppendFormat("where {0} ", where);
                }
                if (!groupBy.IsNullEmpty())
                {
                    builder.AppendFormat("group by {0} ", groupBy);
                }
                if (!orderBy.IsNullEmpty())
                {
                    builder.AppendFormat("order by {0} ", orderBy);
                }
            }
            else
            {
                if (!tableName.IsNullEmpty())
                {
                    builder.AppendFormat("{1} from (select top {0} {1} from (select top {3} {1} from {2} ", new object[] { pageSize, fieldList, tableName, pageSize * pageIndex });
                }
                if (!where.IsNullEmpty())
                {
                    builder.AppendFormat("where {0} ", where);
                }
                if (!groupBy.IsNullEmpty())
                {
                    builder.AppendFormat("group by {0} ", groupBy);
                }
                if (!orderBy.IsNullEmpty())
                {
                    builder.AppendFormat("order by {0} ", orderBy);
                }
                builder.AppendFormat(") as Top1 {0}) as Top2 {1} ", "order by " + sb, "order by " + orderBy);
            }
            sql.DataSql = builder.ToString();
            return sql;
        }
    }
}

